const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const errorMiddleware = require('./middlewares/errorMiddleware');

// 解析 application/json 格式的请求体
app.use(bodyParser.json());

// 解析 application/x-www-form-urlencoded 格式的请求体
app.use(bodyParser.urlencoded({ extended: true }));

// 引入路由
const goodsRouter = require('./service/goods');
const cartRouter = require('./service/cart');
const userRouter = require('./service/user');

// 将路由挂载到对应路径
app.use('/goods', goodsRouter);
app.use('/cart', cartRouter);
app.use('/user', userRouter);

// 添加全局异常捕获中间件
app.use(errorMiddleware);

// 启动服务器
const port = 3000;
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});
